﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Dukey.Model;
using Dukey.DBUtility;

namespace BLL
{
    /// <summary>
    /// 版块表
    /// </summary>
    public class Boards
    {        
        public static readonly Boards instance = new Boards();

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int bid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Boards");
            strSql.Append(" where bid=@bid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@bid", OleDbType.Integer,4)};
            parameters[0].Value = bid;

            return DbHelper.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Dukey.Model.Boards model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Boards(");
            strSql.Append("cname,ename,fid)");
            strSql.Append(" values (");
            strSql.Append("@cname,@ename,@fid)");
            OleDbParameter[] parameters = {					
					new OleDbParameter("@cname", OleDbType.VarChar,50),
					new OleDbParameter("@ename", OleDbType.VarChar,50),
                    new OleDbParameter("@fid", OleDbType.Integer,4)                            
				};
            parameters[0].Value = model.cname;
            parameters[1].Value = model.ename;
            parameters[2].Value = model.fid;
            try
            {
                DbHelper.ExecuteSql(strSql.ToString(), parameters);
                return DbHelper.GetMaxID("bid", "Boards");
            }
            catch(Exception ex)
            {
                throw ex;
                return 0;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Dukey.Model.Boards model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Boards set ");
            strSql.Append("bid=@bid,");
            strSql.Append("cname=@cname,");
            strSql.Append("ename=@ename,");
            strSql.Append("fid=@fid,");
            strSql.Append("note_num=@note_num,");
            strSql.Append("linkto=@linkto,");
            strSql.Append("showid=@showid,");
            strSql.Append("px=@px,");
            strSql.Append("page_title=@page_title,");
            strSql.Append("remark=@remark,");
            strSql.Append("isshow=@isshow,");
            strSql.Append("ismain=@ismain,");
            strSql.Append("cur_time=@cur_time");
            strSql.Append(" where bid=@bid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@bid", OleDbType.Integer,4),
					new OleDbParameter("@cname", OleDbType.VarChar,50),
					new OleDbParameter("@ename", OleDbType.VarChar,50),
					new OleDbParameter("@fid", OleDbType.Integer,4),
					new OleDbParameter("@note_num", OleDbType.Integer,4),
					new OleDbParameter("@linkto", OleDbType.VarChar,100),
					new OleDbParameter("@showid", OleDbType.Integer,4),
					new OleDbParameter("@px", OleDbType.Integer,4),
					new OleDbParameter("@page_title", OleDbType.VarChar,255),
					new OleDbParameter("@remark", OleDbType.VarChar,250),
					new OleDbParameter("@isshow", OleDbType.Boolean,1),
					new OleDbParameter("@ismain", OleDbType.TinyInt,1),
					new OleDbParameter("@cur_time", OleDbType.Date)};
            parameters[0].Value = model.bid;
            parameters[1].Value = model.cname;
            parameters[2].Value = model.ename;
            parameters[3].Value = model.fid;
            parameters[4].Value = model.note_num;
            parameters[5].Value = model.link;
            parameters[6].Value = model.showid;
            parameters[7].Value = model.px;
            parameters[8].Value = model.page_title;
            parameters[9].Value = model.remark;
            parameters[10].Value = model.isshow;
            parameters[11].Value = model.ismain;
            parameters[12].Value = model.cur_time;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int bid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Boards ");
            strSql.Append(" where bid=@bid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@bid", OleDbType.Integer,4)};
            parameters[0].Value = bid;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Dukey.Model.Boards GetModel(int bid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select bid,cname,ename,fid,xpath,note_num,linkto,showid,px,page_title,remark,isshow,ismain,cur_time from Boards ");
            strSql.Append(" where bid=@bid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@bid", OleDbType.Integer,4)};
            parameters[0].Value = bid;

            Dukey.Model.Boards model = new Dukey.Model.Boards();
            OleDbDataReader dr = DbHelper.ExecuteReader(strSql.ToString(), parameters);

            if (dr.Read())
            {
                if (dr["bid"].ToString() != "")
                {
                    model.bid = int.Parse(dr["bid"].ToString());
                }
                model.cname = dr["cname"].ToString();
                model.ename = dr["ename"].ToString();
                if (dr["fid"].ToString() != "")
                {
                    model.fid = int.Parse(dr["fid"].ToString());
                }
                if (dr["note_num"].ToString() != "")
                {
                    model.note_num = int.Parse(dr["note_num"].ToString());
                }
                model.xpath = dr["xpath"].ToString();
                model.link = dr["linkto"].ToString();
                if (dr["showid"].ToString() != "")
                {
                    model.showid = int.Parse(dr["showid"].ToString());
                }
                if (dr["px"].ToString() != "")
                {
                    model.px = int.Parse(dr["px"].ToString());
                }
                model.page_title = dr["page_title"].ToString();
                model.remark = dr["remark"].ToString();
                if (dr["isshow"].ToString() != "")
                {
                    if ((dr["isshow"].ToString() == "1") || (dr["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (dr["ismain"].ToString() != "")
                {
                    if ((dr["ismain"].ToString() == "1") || (dr["ismain"].ToString().ToLower() == "true"))
                    {
                        model.ismain = true;
                    }
                    else
                    {
                        model.ismain = false;
                    }
                }
                if (dr["cur_time"].ToString() != "")
                {
                    model.cur_time = DateTime.Parse(dr["cur_time"].ToString());
                }
                dr.Dispose();
                return model;
            }
            else
            {
                dr.Dispose();
                return null;
            }
        }

      

        public List<Dukey.Model.Boards> GetList(string strWhere)
        {
            List<Dukey.Model.Boards> list = new List<Dukey.Model.Boards>();
            StringBuilder strSql = new StringBuilder();

            Dukey.Model.WebConfig mysite = BLL.WebConfig.instance.GetModelByCache();
            if(mysite!=null)
            {
                if (mysite.showtype == 2)//表示静态页面
                {
                    strSql.Append("select bid,cname,ename,fid,xpath,note_num,linktohtml as link,showid,px,page_title,remark,isshow,ismain,cur_time,linkto,linktohtml ");
                }
                else
                {
                    strSql.Append("select bid,cname,ename,fid,xpath,note_num,linkto as link,showid,px,page_title,remark,isshow,ismain,cur_time,linkto,linktohtml ");
                }
            }
            strSql.Append(" FROM Boards ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by px,bid");
            
            OleDbDataReader dr = DbHelper.ExecuteReader(strSql.ToString());
            while (dr.Read())
            {
                Dukey.Model.Boards model = new Dukey.Model.Boards();
                if (dr["bid"].ToString() != "")
                {
                    model.bid = int.Parse(dr["bid"].ToString());
                }
                model.cname = dr["cname"].ToString();
                model.ename = dr["ename"].ToString();
                if (dr["fid"].ToString() != "")
                {
                    model.fid = int.Parse(dr["fid"].ToString());
                }
                if (dr["note_num"].ToString() != "")
                {
                    model.note_num = int.Parse(dr["note_num"].ToString());
                }
                model.xpath = dr["xpath"].ToString();
                model.link = dr["link"].ToString();
                if (dr["showid"].ToString() != "")
                {
                    model.showid = int.Parse(dr["showid"].ToString());
                }
                if (dr["px"].ToString() != "")
                {
                    model.px = int.Parse(dr["px"].ToString());
                }
                model.page_title = dr["page_title"].ToString();
                model.remark = dr["remark"].ToString();
                if (dr["isshow"].ToString() != "")
                {
                    if ((dr["isshow"].ToString() == "1") || (dr["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (dr["ismain"].ToString() != "")
                {
                    if ((dr["ismain"].ToString() == "1") || (dr["ismain"].ToString().ToLower() == "true"))
                    {
                        model.ismain = true;
                    }
                    else
                    {
                        model.ismain = false;
                    }
                }
                if (dr["cur_time"].ToString() != "")
                {
                    model.cur_time = DateTime.Parse(dr["cur_time"].ToString());
                }
                list.Add(model);
                model.linkto = dr["linkto"].ToString();
                model.linktohtml = dr["linktohtml"].ToString();

            }
            dr.Dispose();
            return list;
        }
        
        public List<Dukey.Model.Boards> GetListByCache(string strWhere)
        {
            object objModel = Common.DataCache.GetCache("BoardList"+StringHelper.ReplaceBadChar(strWhere));
            if (objModel == null)
            {
                try
                {
                    objModel = GetList(strWhere);
                    if (objModel != null)
                    {
                        Common.DataCache.SetCache("BoardList"+StringHelper.ReplaceBadChar(strWhere), objModel);
                    }
                }
                catch { }
            }
            return objModel as List<Dukey.Model.Boards>;         
        }

    }


}
